//
//  XSThumbnailSelectedView.m
//  AudioVideo
//
//  Created by xueshan1 on 2019/10/28.
//  Copyright © 2019 xueshan1. All rights reserved.
//

#import "XSThumbnailSelectedView.h"
#import "XSThumbnailSelectedViewCollectionViewCell.h"

@interface XSThumbnailSelectedView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation XSThumbnailSelectedView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self = [[[NSBundle mainBundle]loadNibNamed:@"XSThumbnailSelectedView" owner:nil options:nil] firstObject];
        self.frame = frame;
       
        self.flowLayout.itemSize = CGSizeMake(80, 80);
        self.flowLayout.minimumInteritemSpacing = 5;
        self.flowLayout.minimumLineSpacing = 5;
        self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self.collectionView registerNib:[UINib nibWithNibName:@"XSThumbnailSelectedViewCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"XSThumbnailSelectedViewCollectionViewCell"];
     }
    return self;
}

- (IBAction)newBtnAction:(id)sender {
    if (_clickNextBtnBlock) {
        _clickNextBtnBlock();
    }
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithCapacity:5];
    }
    return _dataArray;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    XSThumbnailSelectedViewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XSThumbnailSelectedViewCollectionViewCell" forIndexPath:indexPath];
    
    cell.photoModel = self.dataArray[indexPath.row];
    cell.clickDeleteBtnBlock = ^(XSPhotoModel * _Nonnull photoModel) {
        [self.dataArray removeObject:photoModel];
        [self reloadData];
        
        if (self.clickDeleteBtnBlock) {
            self.clickDeleteBtnBlock(photoModel);
        }
    };
    
    return cell;
}

- (void)setIsVideo:(BOOL)isVideo{
    _isVideo = isVideo;
    if (isVideo) {
        self.tiplabel.text = @"已选择视频";
    }else{
        self.tiplabel.text = @"已选择图片";
    }
}

- (void)reloadData{
    [self.collectionView reloadData];
    self.selectedCountLabel.text = [NSString stringWithFormat:@"%ld",self.dataArray.count];
}


@end
